﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BySoft.Server.Common.Enum;
using BySoft.Web.Utility;
using BySoft.Server.Business;
using BySoft.Server.Common.Schema;
using BySoft.Web.Controls;

namespace BySoft.Web.Site.SysManage
{
    public partial class OperationLogList :  AuthBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitControl();
                BindData(0);

            }
        }

        private void InitControl()
        {
            UIHelper.BindSortList(ddlActionType, BaseEnum.ActionTypeList());

            DataTable dt;
            dt = SysModule.GetLeftModuleList();
            ddlModule.DataTextField = SysModuleTable.C_MODULENAME;
            ddlModule.DataValueField = SysModuleTable.C_MODULEID;
            ddlModule.DataSource = dt;
            ddlModule.DataBind();

            UIHelper.InsertFirstItem(ddlModule);
            UIHelper.InsertFirstItem(ddlActionType);
        }

        private int m_count = 0;
        private void BindData(int pageIndex)
        {

            DataTable dtData = SysUseroperationlog.GetOperationLogList(ddlModule.SelectedValue,
                txtUser.Text.Trim(), clpStartDate.Value,
                clpEndDate.Value, ddlActionType.SelectedValue,
                txtKey.Text.Trim(), pageIndex, pcData.PageSize, out m_count);

            pcData.RecordCount = m_count;
            if (dtData.Rows.Count > 0)
            {
                FormateData(dtData);
                grdResult.DataSource = dtData;
                grdResult.DataBind();
                pcData.CurrentIndex = pageIndex;
            }
            else
            {
                UIHelper.BindEmptyGridView(grdResult, dtData);
            }

        }

        private void FormateData(DataTable dtData)
        {
            if (dtData != null)
            {
                SortedList sl = BaseEnum.ActionTypeList();
                string strResult = string.Empty;
                foreach (DataRow row in dtData.Rows)
                {
                    strResult = row[SysUseroperationlogTable.C_ACTIONTYPE].ToString();
                    if (sl.ContainsKey(strResult))
                    {
                        row[SysUseroperationlogTable.C_ACTIONTYPE] = sl[strResult].ToString();
                    }

                }
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData(0);
        }


        protected void pcData_PageIndexClick(object sender, PageIndexClickEventArgs e)
        {
            BindData(e.PageIndex);
        }

    }
}
